// 天气数据模块
const WeatherApp = {
    init() {
        this.bindEvents();
        this.loadWeatherData();
    },

    bindEvents() {
        // 城市定位按钮点击
        document.querySelector('.location-btn').addEventListener('click', () => {
            this.handleLocationRefresh();
        });
    },

    async loadWeatherData() {
        try {
            const data = await this.fetchData();
            this.updateUI(data);
        } catch (error) {
            console.error('数据加载失败:', error);
        }
    },

    async fetchData() {

        //const response = await fetch('https://api.example.com/weather');
        //if (!response.ok) throw new Error('API请求失败');
        //return await response.json();

        // 模拟API请求延迟
        await new Promise(resolve => setTimeout(resolve, 500));

        // TODO: 实际项目中替换为真实API调用
        return {
            current: {
                temp: 28,
                feels_like: 32,
                humidity: "65%",
                wind: "东南风3级",
                aqi: 45
            },
            hourly: [
                { time: "14:00", temp: 28, rain: 15 },
                { time: "15:00", temp: 29, rain: 30 }
            ]
        };
    },

    updateUI(data) {
        // 更新温度显示
        this.animateValue('.current-temp', data.current.temp);
        // 更新AQI进度条
        this.updateAqiProgress(data.current.aqi);
    },

    // 温度数字动画
    animateValue(selector, newValue) {
        const element = document.querySelector(selector);
        element.style.transform = 'scale(1.2)';
        setTimeout(() => {
            element.textContent = newValue;
            element.style.transform = 'scale(1)';
        }, 300);
    },

    // AQI环形进度动画
    updateAqiProgress(aqi) {
        const progress = document.querySelector('.aqi-progress');
        const deg = (aqi / 100) * 360;
        progress.style.background = `conic-gradient(currentColor ${deg}deg, #eee 0)`;
    }
};

// 初始化应用
WeatherApp.init();